package com.zhiqianbao.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhiqianbao.R;
import com.zhiqianbao.adapter.UserAttachmentListAdapter;
import com.zhiqianbao.entity.Result;
import com.zhiqianbao.entity.UserAttachment;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.HttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liang on 2015/12/29.
 */
public class AttachmentListActivity extends BaseActivity {
    /**
     * 附件列表
     */
    ListView listviewAttachment;
    /**
     * 添加按钮
     */
    TextView buttonAddAttachment;
    /**
     * 空信息
     */
    TextView textviewEmpty;
    Gson gson = new Gson();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attachment_list);
        init();
        initView();
    }

    private void initView() {

        listviewAttachment=(ListView)findViewById(R.id.listview_attachment);
         buttonAddAttachment=(TextView)findViewById(R.id.button_add_attachment);
         textviewEmpty=(TextView)findViewById(R.id.textview_empty);
        buttonAddAttachment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AttachmentListActivity.this, TakeAccesssoryPicActivity.class);
                //1 打开添加页面
                startActivityForResult(intent, 1);
            }
        });

        //查询附件接口
        Map<String, String> param = new HashMap<>();
        param.put("token",  Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getToken() : "");
        param.put("userId", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getId() + "" : "");
        new UserAttachmentAsyncTask().execute(param);
    }

    private class UserAttachmentAsyncTask extends AsyncTask<Map, Void, Result<List>> {
        @Override
        protected void onPreExecute() {
            showWaitDialog();
        }
        @Override
        protected Result<List> doInBackground(Map... params) {
            String result = HttpUtils.httpPostString(Constants.API_ROOT + "/user/userAttachments", params[0], AttachmentListActivity.this);
            Result<List> resultObject = Result.fromJson(result, List.class);
            return resultObject;
        }
        @Override
        protected void onPostExecute(Result<List> result) {
            if (result == null) {
                showShortToast1("连接超时");
            }
            try{
                if(Constants.DEFAULT_SUCCESS_CODE.equals(result.getCode())) {
//                showShortToast1(result.getMsg());
                    List<Object> objects = result.getData();
                    //如果没有附件信息
                    if (objects.isEmpty()) {
                        textviewEmpty.setVisibility(View.VISIBLE);
                    } else {
                        textviewEmpty.setVisibility(View.GONE);
                        List<UserAttachment> userAttachments = new ArrayList<>();
                        for (Object obj : objects) {
                            UserAttachment userAttachment = gson.fromJson(gson.toJson(obj), UserAttachment.class);
                            userAttachments.add(userAttachment);
                        }
                        listviewAttachment.setAdapter(new UserAttachmentListAdapter(AttachmentListActivity.this, userAttachments));
                    }
                } else {
//                showShortToast1(result.getMsg());
                }
            }catch (Exception e){

            }

            closeWaitDialog();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case 1:
                //刷新列表
                //查询附件接口
                Map<String, String> param = new HashMap<>();
                param.put("token", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getToken() : "");
                param.put("userId", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getId() + "" : "");
                new UserAttachmentAsyncTask().execute(param);
                break;
        }
    }
}
